import {callFunctionWithRetry} from '../../utils/util'
const app = getApp()

Page({
  data: {
    mapperId: -1,
    isLoaded: false,
    mapperInfo: null
  },
  onLoad(options) {
    this.setData({
      mapperId: options.id
    })

    this.refresh()
  },
  onCopyPlayListURL() {
    wx.setClipboardData({
      data: this.data.mapperInfo.playlistUrl,
      success: () => {
        wx.showToast({
          title: '已复制',
          icon: 'success'
        });
      }
    });
  },
  refresh(){
    wx.showLoading({
      title: '加载中...',
      mask: true
    })

    callFunctionWithRetry(
      'getMapper',
      {
        mapperId: this.data.mapperId
      }).then(res=>{
        const data = res.result.data;
        console.log('Result:', data);
        this.setData({
          isLoaded: true,
          mapperInfo: data
        })
      }).catch(e=>{
        console.error("Failed??", e)
      }).finally(()=>{
        wx.hideLoading()
      })
  },
  onBackTap(e) {
    const pages = getCurrentPages();
    if (pages.length > 1) {
      wx.navigateBack({
        delta: 1
      });
    } else {
      wx.switchTab({
        url: '/pages/home/index'
      });
    }
  }
})